** This dofile makes the results related to Mexican exporter's exit from the US market

/*
Table 4: Capability Cutoff Changes and Number of Partners A. Capability Cutoff Changes

Table A20:  Mexican Exporter’s Exit from the US market: Continuous Binding Measure

Table A22: Mexican Exporter’s Exit from the US market: Alternative Binding Measure
*/

** For Table 4, A. Capability Cutoff Changes
*** Product-Level Difference-in-Difference ***
* Prepare data for 2005 to 2008
use "${output_data}exp_rfc_prod_textile_us20042011.dta", clear
drop if year==2004
drop if year>=2009
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
tostring hs2, replace
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
destring hs2, replace
drop if _merge==2
drop _merge
save "${temp_data}exp_rfc_prod_textile_us20052008.dta", replace

* Prepare data for 2000 to 2004
use "${output_data}exp_rfc_prod_textile20002004.dta", clear
replace rfc=trim(rfc)
gen aaa=substr(rfc,4,1)
destring aaa, replace force
drop if aaa==.
rename rfc rfctransta
merge m:1 rfctransta using "${output_data}transportista_rfclist.dta"
keep if _merge==1
drop _merge
rename rfctransta rfc
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
gen hs2=substr(hs6,1,2)
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
destring hs2, replace
drop if _merge==2
drop _merge
save "${temp_data}exp_rfc_prod_textile20002004_firm.dta", replace

* Preparing data for 2004 and 2001 from 2000-2004 data
use "${temp_data}exp_rfc_prod_textile20002004_firm.dta", clear
rename FEPA_VAL valorendolares
collapse (sum) valor, by(rfc hs6 hs2 maquila year) 
reshape wide valor, i(rfc hs6 hs2 year) j(maquila)
replace valorendolares0=0 if valorendolares0==.
replace valorendolares1=0 if valorendolares1==.
gen valor=valorendolares0+valorendolares1
drop valorendolares**
destring year, replace
preserve
keep if year==2001
save "${temp_data}exp_rfc_prod_textile2001.dta", replace
restore
keep if year==2004
save "${temp_data}exp_rfc_prod_textile2004.dta", replace

* Prepare data for 1998 and 2001
use "${rawdata}export1993_1999.dta", clear
gen year=substr(v2, -4, 4)
destring year, replace
rename v3 hs6
rename v4 rfc
keep if v6=="ESTADOS UNIDOS"
gen hs2=substr(hs6,1,2)
rename v7 valorendolares
collapse (sum) valorendolares, by(rfc hs6 hs2 year)
save "${temp_data}export1993_1999cleaned.dta", replace

use "${temp_data}export1993_1999cleaned.dta", clear
keep if year==1998
rename valor valor
destring hs2, replace force
keep if hs2>=50 & hs2<=63
replace rfc=trim(rfc)
gen aaa=substr(rfc,4,1)
destring aaa, replace force
drop if aaa==.
rename rfc rfctransta
merge m:1 rfctransta using "${output_data}transportista_rfclist.dta"
keep if _merge==1
drop _merge aaa
rename rfctransta rfc
* Append data for 2001
append using "${temp_data}exp_rfc_prod_textile2001.dta"
reshape wide valor, i(rfc hs6) j(year)
drop if valor1998==.
gen exit=0
replace exit=1 if valor1998!=. & valor2001==.
bys hs6: gen nexp=_N
keep if exit==0
collapse nexp (sum) valor1998 (min) minvalue=valor1998 , by(hs6 hs2)
drop if minvalue==0
gen time=0
save "${temp_data}temp9801rank.dta", replace
keep nexp valor1998 hs6
save "${temp_data}temp9801prodcont.dta", replace

* Prepare data on exit, initial year==2001
use "${temp_data}exp_rfc_prod_textile20002004_firm.dta", replace
drop if year=="2000"
rename FEPA_VAL valorendolares
collapse (sum) valor, by(rfc hs6 hs2 maquila year) 
reshape wide valor, i(rfc hs6 hs2 year) j(maquila)
replace valorendolares0=0 if valorendolares0==.
replace valorendolares1=0 if valorendolares1==.
gen valor=valorendolares0+valorendolares1
drop valorendolares**
destring year, replace
reshape wide valor, i(rfc hs6 hs2) j(year)
drop if valor2001==.
gen exit2004=0
replace exit2004=1 if valor2004==.
gsort hs6 -valor2001
by hs6: gen nexp=_N
bys hs6: egen prod_nfirm2001=count(valor2001)
keep if exit2004==0
collapse nexp (sum) valor2001 (min) minvalue=valor2001 , by (hs6 hs2)
drop if minvalue==0
gen time=1
save "${temp_data}temp0104rank.dta", replace
keep nexp valor2001 hs6
save "${temp_data}temp0104prodcont.dta", replace

* Prepare data exit for later panel, initial year==2004
use "${temp_data}exp_rfc_prod_textile_us20052008.dta", clear
collapse (sum) valor, by(rfc hs6 hs2 maquila year) 
reshape wide valor, i(rfc hs6 hs2 year) j(maquila)
replace valorendolares0=0 if valorendolares0==.
replace valorendolares1=0 if valorendolares1==.
gen valor=valorendolares0+valorendolares1
drop valorendolares**
* Append data for initial year 2004
append using "${temp_data}exp_rfc_prod_textile2004.dta"
tostring hs2, replace
reshape wide valor, i(rfc hs6 hs2) j(year)
drop if valor2004==.
gen exit2007=0
replace exit2007=1 if valor2007==.
gsort hs6 -valor2004
by hs6: gen nexp=_N
keep if exit2007==0
collapse nexp (sum) valor2004  (min) minvalue=valor2004 , by (hs6 hs2)
gen time=2
drop if minvalue==0
save "${temp_data}temp0407rank.dta", replace


** Regression, 01-04 against 04-07
use "${temp_data}temp0407rank.dta", replace
destring hs2, replace
append using "${temp_data}temp0104rank.dta"
tostring hs2, replace
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
gen lminvalue=log(minvalue)
gen treatment=0
replace treatment=1 if iq_mex_bind80==1
gen after=time-1
gen treatafter=treatment*(time-1)
rename nexp nexpy
drop valor**
drop _merge
merge m:1 hs6 using "${temp_data}temp0104prodcont.dta"
gen lprod_value=log(valor2001)
gen lprod_value_after=lprod_value*after
gen prod_nfirm=nexp
gen prod_nfirm_after=prod_nfirm*after
drop _merge
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
tostring hs2, replace
* Make variables for product-level controls
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
destring hs2, replace
drop if _merge==2
drop _merge
gen menafter=men*after
gen womanafter=woman*after
gen cottonafter=cotton*after
gen Woolafter=Wool*after
gen manmadeafter=manmade*after
drop hs2
gen hs2=substr(hs6, 1, 2)
destring  hs6, replace
xtset hs6
bys hs6 : gen ntime=_N
keep if ntime==2
rename lminvalue Cutoff

eststo clear
eststo: areg Cutoff treatment after treatafter [aw=nexp] if ntime==2,  absorb(hs2) cluster(hs6)
eststo: areg Cutoff treatment after treatafter men* woman* cotton* Wool* manmade* lprod_value lprod_value_after prod_nfirm prod_nfirm_after [aw=nexp] if ntime==2 ,  absorb(hs2) cluster(hs6)

** Regression, 98-01 against 01-04
use "${temp_data}temp9801rank.dta", replace
destring hs2, replace
append using "${temp_data}temp0104rank.dta"
tostring hs2, replace
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
gen lminvalue=log(minvalue)
gen treatment=0
replace treatment=1 if iq_mex_bind80==1
gen after=time
gen treatafter=treatment*after
rename nexp nexpy
drop  valor**
drop _merge
merge m:1 hs6 using "${temp_data}temp9801prodcont.dta"
gen lprod_value=log(valor1998)
gen lprod_value_after=lprod_value*after
gen prod_nfirm=nexp
gen prod_nfirm_after=prod_nfirm*after
drop _merge
tostring hs6, replace
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
tostring hs2, replace
* Make variables for product-level controls
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
destring hs2, replace
drop if _merge==2
drop _merge
gen menafter=men*after
gen womanafter=woman*after
gen cottonafter=cotton*after
gen Woolafter=Wool*after
gen manmadeafter=manmade*after
drop hs2
gen hs2=substr(hs6, 1, 2)
destring  hs6, replace
xtset hs6
bys hs6 : gen ntime=_N
keep if ntime==2
rename lminvalue Cutoff

eststo: areg Cutoff treatment after treatafter [aw=nexp], absorb(hs2) cluster(hs6)
eststo: areg Cutoff treatment after treatafter men* woman* cotton* Wool* manmade* lprod_value lprod_value_after prod_nfirm prod_nfirm_after [aw=nexp], absorb(hs2) cluster(hs6)

*** Firm-Level Difference-in-Difference ***
* Prepare data on exit, initial year==2001
use "${temp_data}exp_rfc_prod_textile20002004_firm.dta", clear
drop if year=="2000"
rename FEPA_VAL valorendolares
collapse (sum) valor, by(rfc hs6 hs2 maquila year) 
reshape wide valor, i(rfc hs6 hs2 year) j(maquila)
replace valorendolares0=0 if valorendolares0==.
replace valorendolares1=0 if valorendolares1==.
gen valor=valorendolares0+valorendolares1
gen maquilaratio=valorendolares1/valor
drop valorendolares**
destring year, replace
reshape wide valor maquila, i(rfc hs6 hs2) j(year)
drop if valor2001==.
gen exit2004=0
replace exit2004=1 if valor2004==.
drop maquilaratio2002 maquilaratio2003 maquilaratio2004
gsort hs6 -valor2001
by hs6: gen nexp=_N
gen time=1
save "${temp_data}temp0104.dta", replace

* Prepare data exit for later panel, initial year==2004
use "${temp_data}exp_rfc_prod_textile_us20052008.dta", clear
collapse (sum) valor, by(rfc hs6 hs2 maquila year) 
reshape wide valor, i(rfc hs6 hs2 year) j(maquila)
replace valorendolares0=0 if valorendolares0==.
replace valorendolares1=0 if valorendolares1==.
gen valor=valorendolares0+valorendolares1
gen maquilaratio=valorendolares1/valor
drop valorendolares**
append using "${temp_data}exp_rfc_prod_textile2004.dta"
tostring hs2, replace
replace rfc=trim(rfc)
reshape wide valor maquila, i(rfc hs6 hs2) j(year)
drop if valor2004==.
gen exit2007=0
replace exit2007=1 if valor2007==.
drop maquilaratio2005 maquilaratio2006 maquilaratio2007 maquilaratio2008
gsort hs6 -valor2004
by hs6: gen nexp=_N
gen time=2
save "${temp_data}temp0408.dta", replace

** Regression, 01-04 against 04-07
use "${temp_data}temp0408.dta", clear
destring hs2, replace
append using "${temp_data}temp0104.dta"
tostring hs2, replace
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
gen exit010407=exit2004
replace exit010407=exit2007 if exit010407==.
gen treatment=0
replace treatment=1 if iq_mex_bind80==1
gen after=time-1
gen treatafter=treatment*(time-1)
gen maquilaratio=maquilaratio2001
replace maquilaratio=maquilaratio2004 if maquilaratio==.
gen maquilaafter=maquilaratio*after
gen lvalue=log(valor2001)
replace lvalue=log(valor2004) if lvalue==.
gen lvalueafter=lvalue*after
bys hs6: egen prod_value2001=sum(valor2001)
bys hs6: egen prod_value2004=sum(valor2004)
gen lprod_value=log(prod_value2001)
replace lprod_value=log(prod_value2004) if after==1
gen lprod_value_after=lprod_value*after
bys hs6: egen prod_nfirm2001=count(valor2001)
bys hs6: egen prod_nfirm2004=count(valor2004)
gen prod_nfirm=prod_nfirm2001
replace prod_nfirm=prod_nfirm2004 if after==1
gen prod_nfirm_after=prod_nfirm*after
drop _merge
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
tostring hs2, replace
* Make variables for product-level controls
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
destring hs2, replace
drop if _merge==2
drop _merge
gen menafter=men*after
gen womanafter=woman*after
gen cottonafter=cotton*after
gen Woolafter=Wool*after
gen manmadeafter=manmade*after
rename exit010407 Exit

eststo: areg Exit treatment after treatafter lvalue lvalueafter, absorb(hs2) cluster(hs6)
eststo: areg Exit treatment after treatafter lvalue lvalueafter men* woman* cotton* Wool* manmade* lprod_value lprod_value_after prod_nfirm prod_nfirm_after, absorb(hs2) cluster(hs6)

* Prepare data for 1998 and 2001
use "${temp_data}export1993_1999cleaned.dta", clear
keep if year==1998
rename valor valor
destring hs2, replace force
keep if hs2>=50 & hs2<=63
replace rfc=trim(rfc)
gen aaa=substr(rfc,4,1)
destring aaa, replace force
drop if aaa==.
rename rfc rfctransta
merge m:1 rfctransta using "${output_data}transportista_rfclist.dta"
keep if _merge==1
drop _merge aaa
rename rfc rfc
append using "${temp_data}exp_rfc_prod_textile2001.dta"
reshape wide valor, i(rfc hs6) j(year)
drop if valor1998==.
gen exit=0
replace exit=1 if valor1998!=. & valor2001==.
save "${temp_data}temp9801.dta", replace

** Regression, 01-04 against 04-07
use "${temp_data}temp9801.dta", clear
gen after=0
drop valor2001
rename exit exit2004
append using "${temp_data}temp0104.dta"
replace after=1 if time==1
drop nexp
gen value=valor1998 if after==0
replace value=valor2001 if after==1
gen lvalue=log(value)
gen lvalueafter=lvalue*after
rename exit2004 exit980104
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
gen treatment=(iq_mex_bind80==1)
gen treatafter=treatment*after
drop _merge
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
tostring hs2, replace
* Make variables for product-level controls
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
destring hs2, replace
drop if _merge==2
drop _merge
gen menafter=men*after
gen womanafter=woman*after
gen cottonafter=cotton*after
gen Woolafter=Wool*after
gen manmadeafter=manmade*after
destring hs6, replace
xtset hs6
drop valor1998
tostring hs6, replace
merge m:1 hs6 using "${temp_data}temp9801prodcont.dta"
gen lprod_value=log(valor1998)
gen lprod_value_after=lprod_value*after
gen prod_nfirm=nexp
gen prod_nfirm_after=prod_nfirm*after
rename exit980104 Exit

eststo: areg Exit treatment after treatafter lvalue lvalueafter, absorb(hs2) cluster(hs6)
eststo: areg Exit treatment after treatafter lvalue lvalueafter men* woman* cotton* Wool* manmade* lprod_value lprod_value_after prod_nfirm prod_nfirm_after, absorb(hs2) cluster(hs6)

esttab using "${output}firm_exit/mexican_exit.tex", replace coeflabels(treatment "Binding ($\delta_1$)" after "After ($\delta_3$)" treatafter "Binding \times After ($\delta_2$)" lvalue "ln Export ($\delta_4$)" lvalueafter "ln Export \times After ($\delta_5$)") keep(treatment after treatafter lvalue lvalueafter) starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) stats(N, fmt(%9.0f) labels(Obs.)) substitute((d)) nonumbers indicate("Controls=men*") posthead(&\multicolumn{4}{c}{Product-Level Difference-in-Difference}&\multicolumn{4}{c}{Firm-Level Difference-in-Difference}\\ \hline &\multicolumn{4}{c}{ln ExportCutoff_{gsr}}&\multicolumn{4}{c}{Exit_{igsr}}\\ Period 1 & \multicolumn{2}{c}{2001–04} & \multicolumn{2}{c}{1998–2001} & \multicolumn{2}{c}{2001–04} & \multicolumn{2}{c}{1998–2001}\\  \hline Period 1 & \multicolumn{2}{c}{2004–07} & \multicolumn{2}{c}{2001–2004} & \multicolumn{2}{c}{2004–07} & \multicolumn{2}{c}{2001–2004}\\ \hline) nomtitles



** For Table A20:  Mexican Exporter’s Exit from the US market: Continuous Binding Measure
*** Product-Level Difference-in-Difference ***
** Regression, 01-04 against 04-07
use "${temp_data}temp0407rank.dta", replace
destring hs2, replace
append using "${temp_data}temp0104rank.dta"
tostring hs2, replace
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop _merge
gen lminvalue=log(minvalue)
* Set bind80_mex as a Binding measure
gen treatment=bind80_mex
gen after=time-1
gen treatafter=treatment*(time-1)
rename nexp nexpy
drop valor**
merge m:1 hs6 using "${temp_data}temp0104prodcont.dta"
gen lprod_value=log(valor2001)
gen lprod_value_after=lprod_value*after
gen prod_nfirm=nexp
gen prod_nfirm_after=prod_nfirm*after
drop _merge
tostring hs6, replace
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
tostring hs2, replace
* Make variables for product-level controls
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
destring hs2, replace
drop if _merge==2
drop _merge
gen menafter=men*after
gen womanafter=woman*after
gen cottonafter=cotton*after
gen Woolafter=Wool*after
gen manmadeafter=manmade*after
drop hs2
gen hs2=substr(hs6, 1, 2)
destring  hs6, replace
xtset hs6
bys hs6 : gen ntime=_N
keep if ntime==2
rename lminvalue Cutoff

eststo clear
eststo: areg Cutoff treatment after treatafter [aw=nexp] if ntime==2,  absorb(hs2) cluster(hs6)
eststo: areg Cutoff treatment after treatafter men* woman* cotton* Wool* manmade* lprod_value lprod_value_after prod_nfirm prod_nfirm_after [aw=nexp] if ntime==2 ,  absorb(hs2) cluster(hs6)


*** Firm-Level Difference-in-Difference ***
** Regression, 98-01 against 01-04 
use "${temp_data}temp9801rank.dta", replace
destring hs2, replace
append using "${temp_data}temp0104rank.dta"
tostring hs2, replace
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop _merge
gen lminvalue=log(minvalue)
* Set bind80_mex as a Binding measure
gen treatment=bind80_mex
gen after=time
gen treatafter=treatment*after
rename nexp nexpy
drop valor**
merge m:1 hs6 using "${temp_data}temp9801prodcont.dta"
gen lprod_value=log(valor1998)
gen lprod_value_after=lprod_value*after
gen prod_nfirm=nexp
gen prod_nfirm_after=prod_nfirm*after
drop _merge
tostring hs6, replace
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
tostring hs2, replace
* Make variables for product-level controls
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
destring hs2, replace
drop if _merge==2
drop _merge
gen menafter=men*after
gen womanafter=woman*after
gen cottonafter=cotton*after
gen Woolafter=Wool*after
gen manmadeafter=manmade*after
drop hs2
gen hs2=substr(hs6, 1, 2)
destring  hs6, replace
xtset hs6
bys hs6 : gen ntime=_N
keep if ntime==2
rename lminvalue Cutoff

eststo: areg Cutoff treatment after treatafter [aw=nexp], absorb(hs2) cluster(hs6)
eststo: areg Cutoff treatment after treatafter men* woman* cotton* Wool* manmade* lprod_value lprod_value_after prod_nfirm prod_nfirm_after [aw=nexp], absorb(hs2) cluster(hs6)

** Regression, 01-04 against 04-07
use "${temp_data}temp0408.dta", clear
destring hs2, replace
append using "${temp_data}temp0104.dta"
tostring hs2, replace
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop _merge
gen exit010407=exit2004
replace exit010407=exit2007 if exit010407==.
* Set bind80_mex as a Binding measure
gen treatment=bind80_mex
gen after=time-1
gen treatafter=treatment*(time-1)
gen maquilaratio=maquilaratio2001
replace maquilaratio=maquilaratio2004 if maquilaratio==.
gen maquilaafter=maquilaratio*after
gen lvalue=log(valor2001)
replace lvalue=log(valor2004) if lvalue==.
gen lvalueafter=lvalue*after
bys hs6: egen prod_value2001=sum(valor2001)
bys hs6: egen prod_value2004=sum(valor2004)
gen lprod_value=log(prod_value2001)
replace lprod_value=log(prod_value2004) if after==1
gen lprod_value_after=lprod_value*after
bys hs6: egen prod_nfirm2001=count(valor2001)
bys hs6: egen prod_nfirm2004=count(valor2004)
gen prod_nfirm=prod_nfirm2001
replace prod_nfirm=prod_nfirm2004 if after==1
gen prod_nfirm_after=prod_nfirm*after
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
tostring hs2, replace
* Make variables for product-level controls
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
destring hs2, replace
drop if _merge==2
drop _merge
gen menafter=men*after
gen womanafter=woman*after
gen cottonafter=cotton*after
gen Woolafter=Wool*after
gen manmadeafter=manmade*after
rename exit010407 Exit

eststo: areg Exit treatment after treatafter lvalue lvalueafter, absorb(hs2) cluster(hs6)
eststo: areg Exit treatment after treatafter lvalue lvalueafter men* woman* cotton* Wool* manmade* lprod_value lprod_value_after prod_nfirm prod_nfirm_after, absorb(hs2) cluster(hs6)


* Regression, 98-01 against 01-04 
use "${temp_data}temp9801.dta", clear
gen after=0
drop valor2001
rename exit exit2004
append using "${temp_data}temp0104.dta"
replace after=1 if time==1
drop nexp
gen value=valor1998 if after==0
replace value=valor2001 if after==1
gen lvalue=log(value)
gen lvalueafter=lvalue*after
rename exit2004 exit980104
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop _merge
* Set bind80_mex as a Binding measure
gen treatment=bind80_mex
gen treatafter=treatment*after
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
tostring hs2, replace
* Make variables for product-level controls
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
destring hs2, replace
drop if _merge==2
drop _merge
gen menafter=men*after
gen womanafter=woman*after
gen cottonafter=cotton*after
gen Woolafter=Wool*after
gen manmadeafter=manmade*after
destring hs6, replace
xtset hs6
drop valor1998
tostring hs6, replace
merge m:1 hs6 using "${temp_data}temp9801prodcont.dta"
gen lprod_value=log(valor1998)
gen lprod_value_after=lprod_value*after
gen prod_nfirm=nexp
gen prod_nfirm_after=prod_nfirm*after
rename exit980104 Exit

eststo: areg Exit treatment after treatafter lvalue lvalueafter, absorb(hs2) cluster(hs6)
eststo: areg Exit treatment after treatafter lvalue lvalueafter  men* woman* cotton* Wool* manmade* lprod_value lprod_value_after prod_nfirm prod_nfirm_after, absorb(hs2) cluster(hs6)


esttab using "${output}firm_exit/mexican_exit_cont_binding_measure.tex", replace coeflabels(treatment "Binding ($\delta_1$)" after "After ($\delta_3$)" treatafter "Binding \times After ($\delta_2$)" lvalue "ln Export ($\delta_4$)" lvalueafter "ln Export \times After ($\delta_5$)") keep(treatment after treatafter lvalue lvalueafter) starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) stats(N, fmt(%9.0f) labels(Obs.)) substitute((d)) nonumbers indicate("Controls=men*") posthead(&\multicolumn{4}{c}{Product-Level Difference-in-Difference}&\multicolumn{4}{c}{Firm-Level Difference-in-Difference}\\ \hline &\multicolumn{4}{c}{ln ExportCutoff_{gsr}}&\multicolumn{4}{c}{Exit_{igsr}}\\ Period 1 & \multicolumn{2}{c}{2001–04} & \multicolumn{2}{c}{1998–2001} & \multicolumn{2}{c}{2001–04} & \multicolumn{2}{c}{1998–2001}\\  \hline Period 1 & \multicolumn{2}{c}{2004–07} & \multicolumn{2}{c}{2001–2004} & \multicolumn{2}{c}{2004–07} & \multicolumn{2}{c}{2001–2004}\\ \hline) nomtitles




** For Table A22: Mexican Exporter’s Exit from the US market: Alternative Binding Measure
*** Product-Level Difference-in-Difference ***
** Regression, 01-04 against 04-07
use "${temp_data}temp0407rank.dta", replace
destring hs2, replace
append using "${temp_data}temp0104rank.dta"
tostring hs2, replace
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop _merge
gen lminvalue=log(minvalue)
* Set iq_china_bind80 as a Binding measure
gen treatment=(iq_china_bind80==1)
gen after=time-1
gen treatafter=treatment*(time-1)
rename nexp nexpy
drop valor**
merge m:1 hs6 using "${temp_data}temp0104prodcont.dta"
gen lprod_value=log(valor2001)
gen lprod_value_after=lprod_value*after
gen prod_nfirm=nexp
gen prod_nfirm_after=prod_nfirm*after
drop _merge
tostring hs6, replace
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
tostring hs2, replace
* Make variables for product-level controls
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
destring hs2, replace
drop if _merge==2
drop _merge
gen menafter=men*after
gen womanafter=woman*after
gen cottonafter=cotton*after
gen Woolafter=Wool*after
gen manmadeafter=manmade*after
drop hs2
gen hs2=substr(hs6, 1, 2)
destring  hs6, replace
xtset hs6
bys hs6 : gen ntime=_N
keep if ntime==2
rename lminvalue Cutoff

eststo clear
eststo: areg Cutoff treatment after treatafter [aw=nexp] if ntime==2,  absorb(hs2) cluster(hs6)
eststo: areg Cutoff treatment after treatafter men* woman* cotton* Wool* manmade* lprod_value lprod_value_after prod_nfirm prod_nfirm_after [aw=nexp] if ntime==2,  absorb(hs2) cluster(hs6)

*** Firm-Level Difference-in-Difference ***
* Regression, 98-01 against 01-04 
use "${temp_data}temp9801rank.dta", replace
destring hs2, replace
append using "${temp_data}temp0104rank.dta"
tostring hs2, replace
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop _merge
gen lminvalue=log(minvalue)
* Set iq_china_bind80 as a Binding measure
gen treatment=(iq_china_bind80==1)
gen after=time
gen treatafter=treatment*after
rename nexp nexpy
drop valor**
merge m:1 hs6 using "${temp_data}temp9801prodcont.dta"
gen lprod_value=log(valor1998)
gen lprod_value_after=lprod_value*after
gen prod_nfirm=nexp
gen prod_nfirm_after=prod_nfirm*after
drop _merge
tostring hs6, replace
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
tostring hs2, replace
* Make variables for product-level controls
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
destring hs2, replace
drop if _merge==2
drop _merge
gen menafter=men*after
gen womanafter=woman*after
gen cottonafter=cotton*after
gen Woolafter=Wool*after
gen manmadeafter=manmade*after
drop hs2
gen hs2=substr(hs6, 1, 2)
destring  hs6, replace
xtset hs6
bys hs6 : gen ntime=_N
keep if ntime==2
rename lminvalue Cutoff

eststo: areg Cutoff treatment after treatafter [aw=nexp], absorb(hs2) cluster(hs6)
eststo: areg Cutoff treatment after treatafter men* woman* cotton* Wool* manmade* lprod_value lprod_value_after prod_nfirm prod_nfirm_after [aw=nexp], absorb(hs2) cluster(hs6)

* Regression, 01-04 against 04-07
use "${temp_data}temp0408.dta", clear
destring hs2, replace
append using "${temp_data}temp0104.dta"
tostring hs2, replace
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop _merge
gen exit010407=exit2004
replace exit010407=exit2007 if exit010407==.
* Set iq_china_bind80 as a Binding measure
gen treatment=(iq_china_bind80==1)
gen after=time-1
gen treatafter=treatment*(time-1)
gen maquilaratio=maquilaratio2001
replace maquilaratio=maquilaratio2004 if maquilaratio==.
gen maquilaafter=maquilaratio*after
gen lvalue=log(valor2001)
replace lvalue=log(valor2004) if lvalue==.
gen lvalueafter=lvalue*after
bys hs6: egen prod_value2001=sum(valor2001)
bys hs6: egen prod_value2004=sum(valor2004)
gen lprod_value=log(prod_value2001)
replace lprod_value=log(prod_value2004) if after==1
gen lprod_value_after=lprod_value*after
bys hs6: egen prod_nfirm2001=count(valor2001)
bys hs6: egen prod_nfirm2004=count(valor2004)
gen prod_nfirm=prod_nfirm2001
replace prod_nfirm=prod_nfirm2004 if after==1
gen prod_nfirm_after=prod_nfirm*after
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
tostring hs2, replace
* Make variables for product-level controls
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
destring hs2, replace
drop if _merge==2
drop _merge
gen menafter=men*after
gen womanafter=woman*after
gen cottonafter=cotton*after
gen Woolafter=Wool*after
gen manmadeafter=manmade*after
rename exit010407 Exit

eststo: areg Exit treatment after treatafter lvalue lvalueafter, absorb(hs2) cluster(hs6)
eststo: areg Exit treatment after treatafter lvalue lvalueafter men* woman* cotton* Wool* manmade* lprod_value lprod_value_after prod_nfirm prod_nfirm_after , absorb(hs2) cluster(hs6)


** Regression, 98-01 against 01-04 
use "${temp_data}temp9801.dta", clear
gen after=0
drop valor2001
rename exit exit2004
append using "${temp_data}temp0104.dta"
replace after=1 if time==1
drop nexp
gen value=valor1998 if after==0
replace value=valor2001 if after==1
gen lvalue=log(value)
gen lvalueafter=lvalue*after
rename exit2004 exit980104
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop _merge
* Set iq_china_bind80 as a Binding measure
gen treatment=(iq_china_bind80==1)
gen treatafter=treatment*after
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
tostring hs2, replace
* Make variables for product-level controls
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
destring hs2, replace
drop if _merge==2
drop _merge
gen menafter=men*after
gen womanafter=woman*after
gen cottonafter=cotton*after
gen Woolafter=Wool*after
gen manmadeafter=manmade*after
destring hs6, replace
xtset hs6
drop valor1998
tostring hs6, replace
merge m:1 hs6 using "${temp_data}temp9801prodcont.dta"
gen lprod_value=log(valor1998)
gen lprod_value_after=lprod_value*after
gen prod_nfirm=nexp
gen prod_nfirm_after=prod_nfirm*after
rename exit980104 Exit

eststo: areg Exit treatment after treatafter lvalue lvalueafter, absorb(hs2) cluster(hs6)
eststo: areg Exit treatment after treatafter lvalue lvalueafter  men* woman* cotton* Wool* manmade* lprod_value lprod_value_after prod_nfirm prod_nfirm_after , absorb(hs2) cluster(hs6)

esttab using "${output}firm_exit/mexican_exit_alternative_binding_measure.tex", replace coeflabels(treatment "Binding ($\delta_1$)" after "After ($\delta_3$)" treatafter "Binding \times After ($\delta_2$)" lvalue "ln Export ($\delta_4$)" lvalueafter "ln Export \times After ($\delta_5$)") keep(treatment after treatafter lvalue lvalueafter) starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) stats(N, fmt(%9.0f) labels(Obs.)) substitute((d)) nonumbers indicate("Controls=men*") posthead(&\multicolumn{4}{c}{Product-Level Difference-in-Difference}&\multicolumn{4}{c}{Firm-Level Difference-in-Difference}\\ \hline &\multicolumn{4}{c}{ln ExportCutoff_{gsr}}&\multicolumn{4}{c}{Exit_{igsr}}\\ Period 1 & \multicolumn{2}{c}{2001–04} & \multicolumn{2}{c}{1998–2001} & \multicolumn{2}{c}{2001–04} & \multicolumn{2}{c}{1998–2001}\\  \hline Period 1 & \multicolumn{2}{c}{2004–07} & \multicolumn{2}{c}{2001–2004} & \multicolumn{2}{c}{2004–07} & \multicolumn{2}{c}{2001–2004}\\ \hline) nomtitles
